package com.zjlab.web.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author xue
 * @create 2022-11-03 11:26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ResultTag.SUCCESS, "success");
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultTag.SUCCESS, "success");
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ResultTag.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ResultTag.FAIL, "fail");
    }

    public static <T> R<T> failedMsg(String msg) {
        return restResult(null, ResultTag.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ResultTag.FAIL, "fail");
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ResultTag.FAIL, msg);
    }

    public static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiR = new R<>();
        apiR.setCode(code);
        apiR.setData(data);
        apiR.setMsg(msg);
        return apiR;
    }

}
